package com.fg.cloud.factory.controller.report;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.report.OrderAmountReportEntity;
import com.fg.cloud.manager.service.report.OrderAmountReportService;


/**
  * Description:订单资金报表Controller
  * @author shaoqiping
  * @date 2017年9月11日下午6:02:51
*/
@Controller
@RequestMapping("orderAmountReport")
public class OrderAmountReportController extends BaseController {
	@Autowired
	private OrderAmountReportService amountReportService;
	
	/**
	  * Description:展现列表
	  * @param request
	  * @param response
	  * @param page
	  * @param rows
	  * @param sort
	  * @param order
	  * @param filterText
	  * @param status
	  * @author shaoqiping
	  * @date 2017年9月11日下午6:08:23
	*/
	@RequestMapping("/showList")
	@ResponseBody
	public void showList(HttpServletRequest request,HttpServletResponse response, Integer page, Integer rows,
			String sort, String order, String filterText,Integer status,String startTime,String endTime){
		try {
			// page默认为1 rows默认为10
			if (page == null || page <= 0) {
				page = 1;
			}
			if (rows == null || rows <= 0) {
				rows = 10;
			}
			PageUtils<OrderAmountReportEntity> pageinfo = new PageUtils<OrderAmountReportEntity>(page, rows, sort, order);
			Map<String, Object> condition = pageinfo.getCondition();
			condition.put("filterText", filterText);
			condition.put("facId", getCurrentFactoryUser().getFacId());
			condition.put("status", status);
			condition.put("startTime", startTime);
			condition.put("endTime", endTime);
			pageinfo.setCondition(condition);
			amountReportService.findList(pageinfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	
	/**
	  * Description:显示总金额
	  * @param request
	  * @param response
	  * @author shaoqiping
	  * @date 2017年9月12日上午9:14:55
	*/
	@RequestMapping("/showAmount")
	@ResponseBody
	public void showAmount(HttpServletRequest request,HttpServletResponse response){
		try {
			List<OrderAmountReportEntity> lists = amountReportService.findAll(getCurrentFactoryUser().getFacId());
			BigDecimal allPay=BigDecimal.ZERO;
			BigDecimal allReceived=BigDecimal.ZERO;
			for (OrderAmountReportEntity entity : lists) {
				allPay=allPay.add(entity.getAmountPay());
				allReceived=allReceived.add(entity.getAmountPay().subtract(entity.getAmountUnreceived()));
			}
			Map<String,BigDecimal> map = new HashMap<String,BigDecimal>();
			map.put("allPay", allPay);
			map.put("allReceived", allReceived);
			map.put("allUnreceived", allPay.subtract(allReceived));
			doResult(response, true, StatusCode.FINDSUCCESS, map, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
		
	}
}
